import pymysql

class Randomuser:
    # 开启连接#
    def __init__(self):
        self.coon = pymysql.connect('127.0.0.1', 'root', '123456', 'sina')
        self.cursor = self.coon.cursor()

    # 查询
    def randomuserid(self):
        sql='''
        SELECT sw.userid FROM sina_weibo AS sw
        JOIN 
        (SELECT 
            ROUND(
                RAND() * ((SELECT MAX(id) FROM sina_weibo)-(SELECT MIN(id) FROM sina_weibo)) 
                + (SELECT MIN(id) FROM sina_weibo)
            ) AS id
        ) AS sw2 
        WHERE sw.id>=sw2.id
        '''
        self.cursor.execute(sql)
        return self.cursor.fetchone()[0]
    def close_mysql(self):
       # 关闭查询
        self.cursor.close()
        self.coon.close()

if __name__ == '__main__':
    a=Randomuser()
    p=a.randomuserid()
    print(p)